class Region < ActiveRecord::Base
  has_many :artists, :conditions => 'artists.published = true'
  has_many :medias
  has_many :region_descriptions
  has_and_belongs_to_many :users
  has_many :artworks, :through => :artists
 
  validates_presence_of :name
  
  acts_as_ferret :fields => [:name]

  def to_param
    id.to_s + '-' + CGI.escape(name.downcase.gsub(' ', '-'))
  end  
  
  def get_artworks(type, count = 4)
    case type
      when :new
        artworks.find(:all, :order => 'id DESC', :limit => count)
      when :random
        artworks.find(:all, :order => 'rand()', :limit => count)      
      else
        raise "Type #{type.to_s} not valid."
    end
  end
end
